import regeneratorRuntime from '../../lib/runtime/runtime';
import { request } from "../../request/index.js";
// pages/goods_list/goods_list.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs:[
            {
                id:0,
                value:"综合",
                isActive:true
            },
            {
                id:1,
                value:"销量",
                isActive:false
            }, {
                id:2,
                value:"价格",
                isActive:false
            }
        ],
        goodslist:[]
    },
    //接口要的参数
    QueryParams:{
        query:"",
        cid:"",
        pagenum:1,
        pagesize:15
    },
    /* 总页数 */
    totalPages:1,

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.QueryParams.cid=options.cid||"";
        this.QueryParams.query=options.query||"";
        this.getgoodslist()
    },
    handletabsItemChange(e){
        //1.获取被点击的索引
        const {index}=e.detail;
        let {tabs}=this.data;
        tabs.forEach((v,i) =>i===index?v.isActive=true:v.isActive=false);
        this.setData({
            tabs
        })
    },
    /* 页面上滑滚动条触底事件 */
    onReachBottom(){
        if(this.QueryParams.pagenum>=this.totalPages){
           wx:wx.showToast({
             title: '到底了喔！',
           })
        }
        else{
            this.QueryParams.pagenum++;
            this.getgoodslist()
        }
    },
    /* 页面下拉 */
    onPullDownRefresh(){
        /* 重置数组 */
       this.setData({
        goodslist:[]
       })
       /* 重置页码 */
       this.QueryParams.pagenum=1
       /* 发送新请求 */
       this.getgoodslist()
    },
    async getgoodslist(){
        const res=await request({url:"/goods/search",data:this.QueryParams});
        const total=res.total;
        this.totalPages=Math.ceil(total/this.QueryParams.pagesize);

        this.setData({
            /* 拼接的数组 */
            goodslist:[...this.data.goodslist,...res.goods]
        })

        /* 关闭下拉刷新 */
        wx:wx.stopPullDownRefresh()
    }

})